#include "I2S_Audio.h"


// static void init_microphone(void);
// static void init_speaker(void);
//static const char *TAG = "dez_WalkieTalkie";

// /**
//  * @brief  initialize mic
//  */
// static void init_microphone(void)
// {

//     i2s_config_t mic_i2s_config = {
//         .mode = I2S_MODE_MASTER | I2S_MODE_RX,
//         .sample_rate = CONFIG_SAMPLE_RATE,
//         .bits_per_sample = I2S_BITS_PER_SAMPLE_32BIT,//I2S_BITS_PER_SAMPLE_32BIT
//         .channel_format = I2S_CHANNEL_FMT_ONLY_RIGHT,
//         .communication_format = I2S_COMM_FORMAT_STAND_I2S,
//         .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
//         .dma_buf_count = 2,
//         .dma_buf_len = 256,
//         .use_apll = 0,
//     };

//     i2s_pin_config_t mic_pin_config = {
//         .bck_io_num = 5,
//         .ws_io_num = 18,
//         .data_out_num = I2S_PIN_NO_CHANGE,
//         .data_in_num = 4,
//     };

//     ESP_ERROR_CHECK(i2s_driver_install(CONFIG_I2S_CH0, &mic_i2s_config, 0, NULL));
//     ESP_ERROR_CHECK(i2s_set_pin(CONFIG_I2S_CH0, &mic_pin_config));
//     ESP_ERROR_CHECK(i2s_set_clk(CONFIG_I2S_CH0, CONFIG_SAMPLE_RATE, I2S_BITS_PER_SAMPLE_32BIT, I2S_CHANNEL_MONO));
// }

// /**
//  * @brief  initialize speaker
//  */
// static void init_speaker(void)
// {

//     i2s_config_t spk_i2s_config = {
//         .mode = I2S_MODE_MASTER | I2S_MODE_TX,
//         .sample_rate = CONFIG_SAMPLE_RATE,
//         .bits_per_sample = I2S_BITS_PER_SAMPLE_32BIT,//I2S_BITS_PER_SAMPLE_32BIT
//         .channel_format = I2S_CHANNEL_FMT_ONLY_RIGHT,
//         .communication_format = I2S_COMM_FORMAT_STAND_I2S,
//         .dma_buf_count = 2,
//         .dma_buf_len = 256,
//         .use_apll = false,
//         .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1};

//     i2s_pin_config_t spk_pin_config = {
//         .bck_io_num = 27,
//         .ws_io_num = 26,
//         .data_out_num = 25,
//         .data_in_num = I2S_PIN_NO_CHANGE,
//     };

//     ESP_ERROR_CHECK(i2s_driver_install(CONFIG_I2S_CH1, &spk_i2s_config, 0, NULL));
//     ESP_ERROR_CHECK(i2s_set_pin(CONFIG_I2S_CH1, &spk_pin_config));
// }

// /**
//  * @brief  initialize I2S
//  */
// void init_i2s_device()
// {
//     samples_32bit = (uint32_t *)malloc(sizeof(uint32_t) * CONFIG_COUNT);
//     samples_8bit = (uint8_t *)malloc(sizeof(uint8_t) * CONFIG_COUNT * 4);
//     // output_32bit = (uint32_t *)malloc(sizeof(uint32_t) * 64);
//     // samples_32bit = (int32_t *)malloc(sizeof(int32_t) * 128);
//     // samples_16bit = (int16_t *)malloc(sizeof(int16_t) * 128);
//     // samples_8bit = (uint8_t *)malloc(sizeof(uint8_t) * 128 * 4);
//     // recive_16bit = (int16_t *)malloc(sizeof(int16_t) * 128);
//     // output_16bit = (int16_t *)malloc(sizeof(int16_t) * 128);
//     output_32bit = (uint32_t *)malloc(sizeof(uint32_t) * CONFIG_COUNT);

//     init_microphone();
//     //ESP_LOGI(TAG, "初始化麦克风完毕");
//     init_speaker();
//     //ESP_LOGI(TAG, "初始化扬声器完毕");
// }




/**
  * @brief  read audio from i2s
  */
int readVoice(uint8_t *samples, int count, audio_element_handle_t raw_stream_reader) 
{
    // size_t bytes_read = 0;
    // i2s_read(CONFIG_I2S_CH0, (char *)samples_32bit, sizeof(uint32_t) * count, &bytes_read, portMAX_DELAY);
    int bytes_read = 0;
    // ==========32bit=============
    if (AUDIT_BIT == 32) //32bit 采样
    {
        bytes_read = raw_stream_read(raw_stream_reader, (char *)samples_32bit, sizeof(uint32_t) * count);
        //printf("bytes_read %d",bytes_read);
        int samples_read = bytes_read / sizeof(uint32_t);

        int k = 0;
        for (int i = 0; i < samples_read; i++)
        {
            k = i * 4;
            samples[k] = (uint8_t)((samples_32bit[i] >> 0));
            samples[k + 1] = (uint8_t)((samples_32bit[i] >> 8));
            samples[k + 2] = (uint8_t)((samples_32bit[i] >> 16));
            samples[k + 3] = (uint8_t)((samples_32bit[i] >> 24));
        }
        // printf("output data %x,%x,%x,%x\n", samples[0], samples[1], samples[2], samples[3]);

    }
    if (AUDIT_BIT == 16) 
    {
        //==========16bit=============
        bytes_read = raw_stream_read(raw_stream_reader, (char *)samples_16bit, sizeof(uint16_t) * count * 2);
        int samples_read = bytes_read / sizeof(uint16_t);

        int k = 0;
        for (int i = 0; i < samples_read; i++)
        {
            k = i * 2;
            samples[k] = (uint8_t)((samples_16bit[i] >> 0));
            samples[k + 1] = (uint8_t)((samples_16bit[i] >> 8));
        }
        //printf("output data %x,%x,%x,%x\n", samples[0], samples[1], samples[2], samples[3]);

    }

    //==========8bit=============
    // int bytes_read=raw_stream_read(raw_stream_reader, (char *)samples, sizeof(uint8_t) * count);  //8bit 采样

    return bytes_read;
}

